

# 导包
import json
from pyecharts.charts import Map
from pyecharts.options import TitleOpts
from pyecharts.options import VisualMapOpts

# 读取文件
with open("D:/python-learn/data/疫情.txt", "r", encoding="UTF-8") as f:
    data = f.read()

# 将JSON文件转化为python字典
data_dict = json.loads(data)

# 取出各省数据
cn_data_dict = data_dict['areaTree'][0]['children']

autonomous_region = ["西藏", "内蒙古"]
sar = ["澳门", "香港"]
municipalities = ["北京", "上海", "重庆", "天津"]
# 取出省总确诊数据与名字
province_confirm_list = []
for province_data in cn_data_dict:
    province_confirm_data = int(province_data['total']['confirm'])
    province_name = province_data['name']
    # 为各省市地区完善名称
    if province_name in sar:
        province_name += "特别行政区"
    elif province_name in municipalities:
        province_name += "市"
    elif province_name in autonomous_region:
        province_name += "自治区"
    elif province_name == "新疆":
        province_name += "维吾尔自治区"
    elif province_name == "宁夏":
        province_name += "回族自治区"
    elif province_name == "广西":
        province_name += "壮族自治区"
    else:
        province_name += "省"
    province_confirm_list.append((province_name,province_confirm_data))    # 不可调转，名字在前，数据在后。否则显示空白
print(province_confirm_list)
# 创建地图对象
map = Map()

# 添加数据
map.add("全国疫情地图",province_confirm_list,"china")
# 设置全局选项
map.set_global_opts(title_opts=TitleOpts(title="全国疫情分布图",
                                         pos_bottom="1%",
                                         pos_left="center"),
                    visualmap_opts=VisualMapOpts(is_show=True,
                                                 is_piecewise=True,
                                                 pieces=[
                                                     {"min":1, "max":99, "label":"1-99", "color":"#CCFFFF"},
                                                     {"min":100, "max":999, "label":"100-999", "color":"#FFFF99"},
                                                     {"min":1000, "max":4999, "label":"1000-4999", "color":"#FF9966"},
                                                     {"min":5000, "max":9999, "label":"5000-9999", "color":"#FF6666"},
                                                     {"min":10000, "max":50000, "label":"10000-50000", "color":"#CC3333"},
                                                     {"min":50001, "label":"50000+", "color":"#990033"}
                                                 ]))

# 生成
map.render("全国疫情确诊分布.html")


